package tunafish2k.rpgex.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.registry.RegistryKeys;
import tunafish2k.rpgex.registries.EffectRegistry;

@Mixin(SwordItem.class)
public class SwordItemMixin {
    /**
     * "postDamageEntity(Lnet/minecraft/item/ItemStack;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/entity/LivingEntity)V"
     */
    @Inject(method = "postDamageEntity", at = @At("RETURN"))
    public void postDamageEntity(ItemStack stack, LivingEntity target, LivingEntity attacker,
            CallbackInfo callbackInfo) {
        int amplifier = Math.max(0,
                (int) Math.floor(Math.log(attacker.getAttributeValue(EntityAttributes.GENERIC_ATTACK_DAMAGE))) - 1);
        target.addStatusEffect(new StatusEffectInstance(attacker.getWorld().getRegistryManager()
                .get(RegistryKeys.STATUS_EFFECT).entryOf(EffectRegistry.BLEEDING_KEY), 60, amplifier));
    }    
}
